package com.cgycms.webdeploy.filter;

import com.cgycms.webdeploy.auth.UserModel;
import com.cgycms.webdeploy.common.DeployConstant;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName : LoginInterceptor
 * @Description : 登录拦截器
 * @Author : 王力超
 * @Date: 2020-11-10 10:32
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            UserModel user = (UserModel) request.getSession().getAttribute(DeployConstant.USER_SESSION_ID);
            if (user != null) {
                return true;
            }
            response.sendRedirect(request.getContextPath() + "/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}
